package com.ghck.shop.config;

import java.io.File;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.impl.StaticLoggerBinder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.ghck.shop.utils.FileStoreUtil;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;


/** 
 * @author 作者 : chenxuan
 * @version 创建时间：2015年12月16日 下午3:17:27 
 * 类说明 
 */
@Configuration
public class InitConfiguration {
	
	@Autowired
	private Environment env;
	
	private Logger logger = org.slf4j.LoggerFactory.getLogger(InitConfiguration.class);
	
	public static String PINGXX_APP_ID = null;
	
	@Configuration
    @Profile(UserProfile.INTG)
    @PropertySource(value = {"classpath:/intg/config.properties"})
    static class ServiceIntgConfiguration {
    }
	
	@Configuration
    @Profile(UserProfile.PROD)
    @PropertySource(value = {"classpath:/prod/config.properties"})
    static class ServiceProdConfiguration {
    }
	
	@PostConstruct
	public void init(){
		String[] activeProfile = env.getActiveProfiles();
		if(null == activeProfile || activeProfile.length == 0){
			logger.error("active profile is null , System exit.");
		}
		logger.info("active profile : ".concat(activeProfile[0]));
		logger.info("init application config............ begin");
		configLogger();
		logger.info("init application config............done");
	}
	
	private void configLogger(){
		String logback = env.getProperty("config.logback_file");
		logger.info("logback file path : ".concat(logback));
		if(logback!=null && !logback.isEmpty()){
			File file = new File(logback);
			if(file!=null && file.exists()){
				StaticLoggerBinder loggerBinder = StaticLoggerBinder.getSingleton();
				//  				    LoggerContext loggerContext = (LoggerContext) loggerBinder.getLoggerFactory();
				LoggerContext context = (LoggerContext) loggerBinder.getLoggerFactory();

				try {
					JoranConfigurator configurator = new JoranConfigurator();
					configurator.setContext(context);
					// Call context.reset() to clear any previous configuration, e.g. default 
					// configuration. For multi-step configuration, omit calling context.reset().
					context.reset(); 
					configurator.doConfigure(file);
				} catch (JoranException je) {
//					je.printStackTrace();
					logger.error("Init failed",je);
				} catch (Throwable ex){
					logger.error("Init failed",ex);
//					ex.printStackTrace();
				}
			}
			logger.info("init logger config............done");
		}else{
			logger.info("<<<<<<<<< init logback file error >>>>>>>>>>");
		}
	}
	
	@Bean
	public ConfigUtils configUtils(){
		ConfigUtils util = new ConfigUtils();
		util.setEnvironment(env);
		return util;
	}
	
	@Bean
	public FileStoreUtil fileStoreUtil(){
		FileStoreUtil util = new FileStoreUtil();
		util.setEnv(env);
		return util;
	}

}